<?php
/**
 *	团购页
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class groupbuyControl extends MobileControl{
	
	//定义常量
    const STATE_VERIFY =  1;
    const STATE_CANCEL =  2;
    const STATE_PROGRESS =  3;
    const STATE_VERIFY_FAIL =  4;
    const STATE_CLOSE =  5;
    const TEMPLATE_STATE_ACTIVE = 1;
    const TEMPLATE_STATE_UNACTIVE = 2;

    public function __construct() {
        parent::__construct();
        //检查团购功能是否开启
        if (intval($GLOBALS['setting_config']['groupbuy_allow']) !== 1){
            mobileJump('团购功能暂未开启','index.php');
        }
    }
	
	/**
	 * 默认跳转到进行中的团购列表
	 */
    public function indexOp() {
        $this->groupbuy_listOp();
	}

    /**
     * 进行中的团购
     **/
    public function groupbuy_listOp() {
		$g_cache = ($cache = F('groupbuy'))? $cache : H('groupbuy',true,'file');
        //获取当前进行中的团购活动
        $template_in_progress = $this->get_groupbuy_template_list('in_progress');
		if(empty($template_in_progress) || !is_array($template_in_progress)){
			mobileJump('暂无进行中的团购活动');
		}
		 //输出倒计时
        $this->output_count_down($template_in_progress[0]['end_time']);
        //分页
        $page = new Page();
        $page->setEachNum(10) ;
        $page->setStyle('admin') ;

        //获取正在进行中的团购列表
        $param = array();
		(int)$_GET['groupbuy_area'] > 0 && $param['area_id'] = (int)$_GET['groupbuy_area'];
		(int)$_GET['groupbuy_class'] > 0 && $param['class_id'] = (int)$_GET['groupbuy_class'];

        if(intval($_GET['groupbuy_price']) !== 0) {
            $price_range_list = $g_cache['price'];
            foreach($price_range_list as $price_range) {
                if($price_range['range_id'] == $_GET['groupbuy_price']) {
                    $param['greater_than_groupbuy_price'] = $price_range['range_end'];
                    $param['less_than_groupbuy_price'] = $price_range['range_start'];
                } 
            }
        }
        $groupbuy_order_key = trim($_GET['groupbuy_order_key']);
        $groupbuy_order = empty($_GET['groupbuy_order'])?'desc':trim($_GET['groupbuy_order']);
        if(!empty($groupbuy_order_key)) {
            switch ($groupbuy_order_key) {
                case 'price':
                    $param['order'] = 'state asc,groupbuy_price '.$groupbuy_order;
                    break;
                case 'rebate':
                    $param['order'] = 'state asc,rebate '.$groupbuy_order;
                    break;
                case 'sale':
                    $param['order'] = 'state asc,buyer_count '.$groupbuy_order;
                    break;
            }
        }
		//根据买家用户信息所在地区展示在相应地区展示的商品-市级-省级
		if(isset($_SESSION['is_login']) && (int)$_SESSION['is_login'] == 1){
			$member = Model('member')->infoMember(array('member_id'=>(int)$_SESSION['member_id']));
			$area_info = preg_split('/\t/', $member['member_areainfo'],-1,PREG_SPLIT_NO_EMPTY);
			if(count($area_info)==1){
				$param['show_in_province_ids'] = (int)$member['member_provinceid'];
			}elseif(count($area_info)==2 || count($area_info)==3){
				$param['show_in_city_ids'] = (int)$member['member_cityid'];
			}
		}
        $groupbuy_list = $this->get_groupbuy_list('in_progress',$template_in_progress[0]['template_id'],$page,$param);
        Tpl::output('groupbuy_list',$groupbuy_list);
		$this->_assignMobileData($page);

		//输出页面

        Tpl::output('class_list',$g_cache['category']);
        Tpl::output('area_list',$g_cache['area']);
        Tpl::output('price_list',$g_cache['price']);
		Tpl::output('html_title','团购列表');

		Model('seo')->type('group')->show();

		Tpl::showpage('groupbuy');
    }
	
	 /**
     * 输出倒计时时间
     **/
    private function output_count_down($time) {
        $count_down = intval($time) - time();
        Tpl::output('count_down',$count_down);
    }

    /**
     * 获取不同状态的团购活动
     **/
    private function get_groupbuy_template_list($type,$page='',$param = array()) {

        $model_groupbuy_template =new groupbuy_templateModel();
        $param['state'] = self::TEMPLATE_STATE_ACTIVE;
        switch ($type) {
            case 'in_progress':
                $param['in_progress'] = time();
                break;
            case 'soon':
                $param['less_than_start_time'] = time();
                $param['order'] = 'start_time asc';
                break;
            case 'history':
                $param['greater_than_end_time'] = time();
                break;
            default:
                $param['in_progress'] = time();
                break;
        }
        $template_list = $model_groupbuy_template->getList($param,$page);
        return $template_list;
    }

    /**
     * 获取团购信息列表
     **/
    private function get_groupbuy_list($type,$template_id,$page = '',$param = array()) {
        $model_groupbuy =new goods_groupModel();
        $param['in_template_id'] = $template_id;
        switch ($type) {
            default:
                $param['state_progress_and_close'] = true; 
                break;
        }
        $groupbuy_list = $model_groupbuy->getList($param,$page);
        return $groupbuy_list;
    }

}